<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\CreateUserRequest;
use App\Model\Group;
use App\Model\Site;
use App\Model\User;
use Illuminate\Http\Request;

class GroupController extends Controller
{
    public function getCreateUserGroup(Request $request)
    {
        $user_id = $request->input('user_id');
        $site_id = $request->input('site_id');

        $user = User::find($user_id);
        $site = Site::find($site_id);

        return view('admin.group.create-user-group', [
            'user' => $user,
            'site' => $site
        ]);
    }

    public function postCreateUserGroup(Request $request)
    {
        $params = $request->all();
        $group = Group::create($params);
        if ($group) {
            return $this->json_success($group);
        } else {
            return $this->json_sys_error();
        }
    }

    public function postDeleteGroup(Request $request)
    {
        $id = $request->input('id');
        $group = Group::find($id);
        if (!$group) {
            return $this->json_error('分组不存在', 1001);
        }

        $group->delete();
        return $this->json_success();
    }

}
